package team.unstudio.unknownrpg_base.attribute;

import java.util.HashMap;
import java.util.Map;

import indi.defoliation.unknownrpg.api.attribute.Attribute;
import indi.defoliation.unknownrpg.api.attribute.AttributeBody;
import indi.defoliation.unknownrpg.api.attribute.AttributeEventHandler;
import indi.defoliation.unknownrpg.api.attribute.AttributePriority;
import indi.defoliation.unknownrpg.api.attribute.NumberAttribute;
import indi.defoliation.unknownrpg.api.rpgevent.entity.EntityAttackEvent;

import static team.unstudio.unknownrpg_base.Util.lucky;

public class CritChanceAttribute extends Attribute implements NumberAttribute<Integer>{
	
	private int critChance;
	
	public CritChanceAttribute(int critChance) {
		this.set(critChance);
	}

	@Override
	public Map<String, Object> serialize() {
		HashMap<String,Object> map = new HashMap<>();
		map.put(getKeyName(), critChance);
		return map;
	}

	@Override
	public void add(Integer t) {
		critChance+=t;
		if(critChance>100)
			critChance=100;
	}

	@Override
	public Integer get() {
		return critChance;
	}

	@Override
	public void set(Integer t) {
		critChance=t;
	}

	@Override
	public String getKeyName() {
		return "CritChance";
	}

	@Override
	public String getDisplayName() {
		return "暴击概率";
	}

	@Override
	public void deserialize(Map<String, Object> map) {
		this.critChance=(int)map.get(getKeyName());
	}
	
	@Override
	public Attribute getAttribute() {
		return this;
	}
	
	@AttributeEventHandler(priority = AttributePriority.HIGH,body = {
			AttributeBody.MAIN_HAND,AttributeBody.OFF_HAND})
	public void onAttack(EntityAttackEvent event,Map<AttributeBody,Attribute> map) {
		if(lucky(((BlockChanceAttribute)map.get(AttributeBody.MAIN_HAND)).get())&&
				lucky(((BlockChanceAttribute)map.get(AttributeBody.OFF_HAND)).get())) {
			event.putLetter(this, true);
		}
	}
}
